/**
 * @filename:PrdTaskQtyServiceImpl Dec 22, 2023
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import cn.hutool.core.util.PageUtil;
import com.alibaba.druid.sql.PagerUtils;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.wonder.admin.domain.JobLog;
import com.wonder.admin.domain.Logininfor;
import com.wonder.admin.request.LogininforRequest;
import com.wonder.app.domain.PrdTaskQty;
import com.wonder.app.domain.Proorder;
import com.wonder.app.mapper.PrdTaskQtyDao;
import com.wonder.app.service.PrdTaskQtyService;
import com.wonder.app.request.PrdTaskQtyRequest;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.utils.DateUtils;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**   
 * @Description:TODO(生产订单员工服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class PrdTaskQtyServiceImpl  extends ServiceImpl<PrdTaskQtyDao, PrdTaskQty> implements PrdTaskQtyService  {

    @Autowired
    private PrdTaskQtyDao prdTaskQtyDao;
    /**
     * 验证生产订单员工信息
     * 
     * @param prdTaskQty
     * @param type
     * @return String
     */
    @Override
    public String validate(PrdTaskQty prdTaskQty,String type){
    
    
       return "";
    }

    @Override
    public Map<String,Object> selectWorkTimeList(PrdTaskQtyRequest prdTaskQtyRequest, Page<PrdTaskQty> page) {
        String startTime = "";
        String endTime = "";
        String userName = "";
        if (prdTaskQtyRequest.getCreateTime() != null){
            Date[] createTime = prdTaskQtyRequest.getCreateTime();
            startTime = DateUtils.dateToStr(createTime[0],"yyyy-MM-dd HH:mm:ss");
            endTime = DateUtils.dateToStr(createTime[1],"yyyy-MM-dd HH:mm:ss") + " 23:59:59";
        }

        if(prdTaskQtyRequest.getUserName() != null){
            userName = prdTaskQtyRequest.getUserName();
        }
        long size = page.getSize();
        long pages = page.getCurrent() - 1;
        pages = size * pages;
        List<PrdTaskQty> taskQtyList = prdTaskQtyDao.selectWorkTimeList(startTime,endTime,size,pages,userName);
        //获取总数
        int total = prdTaskQtyDao.selectWorkTimeCount(startTime,endTime,userName);
        Map<String,Object> data = new HashMap<>();
        data.put("data",taskQtyList);
        data.put("total",total);

        return data;
    }

    @Override
    public Map<String, Object> getPieceRote(PrdTaskQtyRequest prdTaskQtyRequest, Page<PrdTaskQty> page) {
        String startTime = "";
        String endTime = "";
        String userName = "";
        if (prdTaskQtyRequest.getCreateTime() != null){
            Date[] createTime = prdTaskQtyRequest.getCreateTime();
            startTime = DateUtils.dateToStr(createTime[0],"yyyy-MM-dd HH:mm:ss");
            endTime = DateUtils.dateToStr(createTime[1],"yyyy-MM-dd") + " 23:59:59";
        }
        if(prdTaskQtyRequest.getUserName() != null){
            userName = prdTaskQtyRequest.getUserName();
        }
        long size = page.getSize();
        long pages = page.getCurrent() - 1;
        pages = size * pages;
        List<PrdTaskQty> taskQtyList = prdTaskQtyDao.getPieceRote(startTime,endTime,size,pages,userName);
        //获取总数
        int total = prdTaskQtyDao.getPieceRoteTotal(startTime,endTime,userName);
        Map<String,Object> data = new HashMap<>();
        data.put("data",taskQtyList);
        data.put("total",total);

        return data;
    }

    @Override
    public Map<String, Object> dumpdataWorkTime(PrdTaskQtyRequest request) {

        long size = request.getLimit();
        long  pages = request.getPage();
        pages = size * pages;
        String startTime = "";
        String endTime = "";
        String userName = "";
        if (request.getCreateTime() != null){
            Date[] createTime = request.getCreateTime();
            startTime = DateUtils.dateToStr(createTime[0],"yyyy-MM-dd HH:mm:ss");
            endTime = DateUtils.dateToStr(createTime[1],"yyyy-MM-dd") + " 23:59:59";
        }
        if(request.getUserName() != null){
            userName = request.getUserName();
        }
        List<PrdTaskQty> list = prdTaskQtyDao.selectWorkTimeList(startTime,endTime, size,pages,userName);
        //整理一下集合信息
        List<VtuzxMap> dataList = new ArrayList<>();
        for (PrdTaskQty taskQty : list){
            String createTime = DateUtils.dateToStr(taskQty.getDayTime(),"yyyy-MM-dd");
            String workType = taskQty.getWorkType();//所属车间
            String ipadUserName = taskQty.getUserName();//员工姓名
            String taskBillNo = taskQty.getTaskBillNo();//生产订单
            Long workTime = taskQty.getWorkTime();//工时
            VtuzxMap data = new VtuzxMap();
            data.append("createTime",createTime)
                    .append("workType",workType)
                    .append("userName",ipadUserName)
                    .append("taskBillNo",taskBillNo)
                    .append("workTime",workTime);
            dataList.add(data);
        }
        String header="日期,所属车间,员工姓名,生产订单,工时";
        Map<String,Object> retMap=new HashMap<String, Object>();
        retMap.put("header", StringUtils.split(header, ','));
        retMap.put("filename", "工时报表.xlsx");
        retMap.put("percentage", list.size());
        retMap.put("data", dataList);
        return AjaxMap.success(retMap);
    }

    @Override
    public Map<String, Object> dumpdataPiece(PrdTaskQtyRequest request) {
        String startTime = "";
        String endTime = "";
        String userName = "";
        if (request.getCreateTime() != null){
            Date[] createTime = request.getCreateTime();
            startTime = DateUtils.dateToStr(createTime[0],"yyyy-MM-dd HH:mm:ss");
            endTime = DateUtils.dateToStr(createTime[1],"yyyy-MM-dd") + " 23:59:59";
        }
        if(request.getUserName() != null){
            userName = request.getUserName();
        }
        long size = request.getLimit();
        long  pages = request.getPage();
        pages = size * pages;
        List<PrdTaskQty> taskQtyList = prdTaskQtyDao.getPieceRote(startTime,endTime,size,pages,userName);
        //整理一下集合信息
        List<VtuzxMap> dataList = new ArrayList<>();
        for (PrdTaskQty taskQty : taskQtyList){
            String createTime = DateUtils.dateToStr(taskQty.getDayTime(),"yyyy-MM-dd");
            String workType = taskQty.getWorkType();//所属车间
            String ipadUserName = taskQty.getUserName();//员工姓名
            String taskBillNo = taskQty.getTaskBillNo();//生产订单
            Double workTime = taskQty.getPiece();//工时
            VtuzxMap data = new VtuzxMap();
            data.append("createTime",createTime)
                    .append("workType",workType)
                    .append("userName",ipadUserName)
                    .append("taskBillNo",taskBillNo)
                    .append("workTime",workTime);
            dataList.add(data);
        }
        String header="日期,所属车间,员工姓名,生产订单,计件分配金额";
        Map<String,Object> retMap=new HashMap<String, Object>();
        retMap.put("header", StringUtils.split(header, ','));
        retMap.put("filename", "计件分配金额.xlsx");
        retMap.put("percentage", dataList.size());
        retMap.put("data", dataList);
        return AjaxMap.success(retMap);
    }

    public IPage<PrdTaskQty> selectList(PrdTaskQtyRequest request, Page<PrdTaskQty> page){
        LambdaQueryWrapper<PrdTaskQty> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        Page<PrdTaskQty> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);

        return selectPage;
    }

}